/* $Id: VBoxMPSa.h $ */

/** @file
 * Sorted array API
 */

/*
 * Copyright (C) 2014-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef _WDDM_INCLUDE_VBOXMPSA_H_
#define _WDDM_INCLUDE_VBOXMPSA_H_

#include "CommonDef.h"

typedef struct CR_SORTARRAY
{
    UINT32 cBufferSize;
    UINT32 cSize;
    UINT64 *pElements;
} CR_SORTARRAY;

int CrSaInit(CR_SORTARRAY *pArray, UINT32 cInitBuffer);
void CrSaCleanup(CR_SORTARRAY *pArray);
/* @return true if element is found */
bool CrSaContains(const CR_SORTARRAY *pArray, UINT64 element);

/*
 * @return VINF_SUCCESS  if element is added
 * VINF_ALREADY_INITIALIZED if element was in array already
 * VERR_NO_MEMORY - no memory
 *  */
int CrSaAdd(CR_SORTARRAY *pArray, UINT64 element);

/*
 * @return VINF_SUCCESS  if element is removed
 * VINF_ALREADY_INITIALIZED if element was NOT in array
 *  */
int CrSaRemove(CR_SORTARRAY *pArray, UINT64 element);

/*
 * @return VINF_SUCCESS on success
 * VERR_NO_MEMORY - no memory
 *  */
void CrSaIntersect(CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2);
int CrSaIntersected(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2, CR_SORTARRAY *pResult);

/*
 * @return VINF_SUCCESS on success
 * VERR_NO_MEMORY - no memory
 *  */
int CrSaUnited(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2, CR_SORTARRAY *pResult);

/*
 * @return VINF_SUCCESS on success
 * VERR_NO_MEMORY - no memory
 *  */
int CrSaClone(const CR_SORTARRAY *pArray1, CR_SORTARRAY *pResult);

int CrSaCmp(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2);
bool CrSaCovers(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2);

UINT32 CrSaGetSize(const CR_SORTARRAY *pArray);
UINT64 CrSaGetVal(const CR_SORTARRAY *pArray, UINT32 i);
void CrSaClear(CR_SORTARRAY *pArray);

#endif
